<?php
require_once 'config.php';

// 初始化数据库连接
try {
    $pdo = new PDO(
        "mysql:host={$host};dbname={$db};charset=utf8mb4",
        $user,
        $pass,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ]
    );
} catch (PDOException $e) {
    die("数据库连接失败: " . $e->getMessage() . "（请检查config.php中的配置）");
}

// 通用数据获取函数
function getFromDatabase($pdo, $sql, $params = []) {
    try {
        $stmt = $pdo->prepare($sql);
        $stmt->execute($params);
        return $stmt->fetchAll();
    } catch (PDOException $e) {
        error_log("数据库查询失败: " . $e->getMessage());
        return [];
    }
}

// 1. 媒体轮播数据
$carouselItems = getFromDatabase(
    $pdo,
    "SELECT id, type, url, title, display_order, is_active, start_time, end_time 
     FROM media_resources  
     WHERE is_active = 1 
       AND (start_time IS NULL OR start_time <= NOW()) 
       AND (end_time IS NULL OR end_time >= NOW())
     ORDER BY display_order ASC 
     LIMIT 5"
);

// 2. 值班信息数据
$today = date('Y-m-d');
$dutyToday = getFromDatabase(
    $pdo,
    "SELECT 
        date AS duty_date,
        week AS duty_week,
        duty_cadre,
        duty_officer,
        Duty_Officer_of_Squad_1 AS squad1,
        Duty_Officer_of_Squad_2 AS squad2,
        Duty_Officer_of_Squad_3 AS squad3,
        Duty_Officer_of_Squad_4 AS squad4
     FROM time_duty 
     WHERE date = ? 
     LIMIT 1",
    [$today]
);
$dutyToday = $dutyToday ? $dutyToday[0] : [];

// 3. 公告数据（减少数量，让通知公告区域更短）
$announcements = getFromDatabase(
    $pdo,
    "SELECT id, title, content, type, created_at AS publish_date, display_order, is_active, updated_at 
     FROM announcements 
     WHERE is_active = 1 
     ORDER BY display_order ASC, updated_at DESC 
     LIMIT 2"  // 从4条减少到2条
);

// 重要通知数据
$importantNotifications = getFromDatabase(
    $pdo,
    "SELECT id, title, content, created_at AS publish_date, display_order 
     FROM announcements 
     WHERE type = 'important' AND is_active = 1 
     ORDER BY display_order ASC, created_at DESC 
     LIMIT 5"
);

// 定义课程时间的正确排序（根据实际需求调整具体文本）|可以通过定义时间顺序映射表来强制指定排序规则
$timeOrder = [
    '第一二节',
    '第三四节',
    '第五六节',
    '第七八节',
    '第九十节'
];
// 4. 课程表相关功能


function getScheduleByWeekday($pdo, $weekday) {
    $thisWeekDate = date('Y-m-d', strtotime("this week $weekday"));
    $nextWeekDate = date('Y-m-d', strtotime("next week $weekday"));
    
    $stmt = $pdo->prepare("
        SELECT *, 
        CASE 
            WHEN course_date = :thisWeekDate1 THEN '本周' 
            ELSE '下周' 
        END AS week_type
        FROM course_schedule 
        WHERE course_date = :thisWeekDate2 OR course_date = :nextWeekDate
        ORDER BY course_time
    ");
    
    $stmt->bindParam(':thisWeekDate1', $thisWeekDate, PDO::PARAM_STR);
    $stmt->bindParam(':thisWeekDate2', $thisWeekDate, PDO::PARAM_STR);
    $stmt->bindParam(':nextWeekDate', $nextWeekDate, PDO::PARAM_STR);
    
    $stmt->execute();
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}



$weekdayMap = [
    'monday' => '周一',
    'tuesday' => '周二',
    'wednesday' => '周三',
    'thursday' => '周四',
    'friday' => '周五',
    'saturday' => '周六',
    'sunday' => '周日'
];

$todayDate = date('Y-m-d');
$dayOfWeek = date('w'); // 0-6 (周日-周六)

$dayOfWeekMap = [
    0 => 'sunday',
    1 => 'monday',
    2 => 'tuesday',
    3 => 'wednesday',
    4 => 'thursday',
    5 => 'friday',
    6 => 'saturday'
];

$todayWeekday = $dayOfWeekMap[$dayOfWeek];
$tomorrowWeekday = $dayOfWeekMap[($dayOfWeek + 1) % 7];

$todaySchedule = getScheduleByWeekday($pdo, $todayWeekday);
$tomorrowSchedule = getScheduleByWeekday($pdo, $tomorrowWeekday);
// 新增：处理今天的课程数据（按时间分组，提取所有班级）
$todayClasses = array_unique(array_column($todaySchedule, 'class_name')); // 动态获取所有班级
sort($todayClasses); // 排序班级
$groupedToday = [];
foreach ($todaySchedule as $row) {
    $time = $row['course_time'];
    $class = $row['class_name'];
    if (!isset($groupedToday[$time])) {
        $groupedToday[$time] = [];
    }
    $groupedToday[$time][$class] = $row; // 按时间+班级存储课程
}
// 按预设的时间顺序重新排序今天的课程
$orderedGroupedToday = [];
foreach ($timeOrder as $time) {
    if (isset($groupedToday[$time])) {
        $orderedGroupedToday[$time] = $groupedToday[$time];
    }
}
$groupedToday = $orderedGroupedToday;


// 新增：处理明天的课程数据（同上）
$tomorrowClasses = array_unique(array_column($tomorrowSchedule, 'class_name'));
sort($tomorrowClasses);
$groupedTomorrow = [];
foreach ($tomorrowSchedule as $row) {
    $time = $row['course_time'];
    $class = $row['class_name'];
    if (!isset($groupedTomorrow[$time])) {
        $groupedTomorrow[$time] = [];
    }
    $groupedTomorrow[$time][$class] = $row;
}
// 按预设的时间顺序重新排序明天的课程
$orderedGroupedTomorrow = [];
foreach ($timeOrder as $time) {
    if (isset($groupedTomorrow[$time])) {
        $orderedGroupedTomorrow[$time] = $groupedTomorrow[$time];
    }
}
$groupedTomorrow = $orderedGroupedTomorrow;
// 5. 奖励表彰与通报批评
$rewards = getFromDatabase(
    $pdo,
    "SELECT id, title, content, created_at AS publish_date, display_order, is_active 
     FROM praises 
     WHERE type = 'praise' 
       AND is_active = 1 
     ORDER BY display_order ASC, created_at DESC 
     LIMIT 5"
);

$punishments = getFromDatabase(
    $pdo,
    "SELECT id, title, content, created_at AS publish_date, display_order, is_active 
     FROM praises 
     WHERE type = 'criticism' 
       AND is_active = 1 
     ORDER BY display_order ASC, created_at DESC 
     LIMIT 5"
);

// 获取当前日期信息
$now = new DateTime();
$weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
$currentWeekday = $weekdays[$now->format('w')];
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1031</title>
    <link rel="stylesheet" href="./resource/css/tailwind.min3.4.17.css">   <!-- 本地CSS路径 -->
    <link rel="stylesheet" href="./resource/css/materialdesignicons.min(1).css">   <!-- 本地CSS路径 -->
    <script src="./resource/js/tailwind.min3.4.17.js"></script> <!-- 本地 JS 文件 -->
    <!-- Tailwind 配置 -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#165DFF',
                        success: '#00B42A',
                        danger: '#F53F3F',
                        warning: '#FF7D00',
                        neutral: '#F5F7FA',
                        dark: '#1D2129',
                        course1: '#E6F4FF',
                        course2: '#F6FFED',
                        course3: '#FFF2E8',
                        course4: '#FFF0F3',
                    },
                    fontFamily: {
                        inter: ['Inter', 'system-ui', 'sans-serif'],
                    },
                    fontSize: {
                        // 缩小所有预设字体大小（原尺寸*0.9）
                        'xs': '0.6rem',    // 原0.75rem(12px) → 约11.2px
                        'sm': '0.7rem',    // 原0.875rem(14px) → 约12.8px
                        'base': '0.8rem',  // 原1rem(16px) → 约14.4px
                        'lg': '0.9rem',   // 原1.125rem(18px) → 约16.8px
                        'xl': '1.1rem',    // 原1.25rem(20px) → 约19.2px
                        '2xl': '1.2rem',  // 原1.5rem(24px) → 约21.6px
                    },
                }
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .scrollbar-hide {
                -ms-overflow-style: none;
                scrollbar-width: none;
            }
            .scrollbar-hide::-webkit-scrollbar {
                display: none;
            }
            .text-shadow {
                text-shadow: 0 2px 4px rgba(0,0,0,0.3);
            }
            .card-hover {
                @apply transition-all duration-300 hover:shadow-lg hover:-translate-y-1;
            }
            .announcement-item strong {
                @apply text-sm;
            }
            .announcement-item p {
                @apply line-clamp-2;
            }
            .carousel-item {
                @apply absolute inset-0 transition-opacity duration-500;
            }
        }
    </style>

    <!-- 课程表样式（美化版） -->
    <style>
        .schedule-container {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: auto;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.08);
    background: #fff;
    border: 1px solid #f0f0f0;
    padding: clamp(10px, 2vw, 15px); /* 内边距随屏幕宽度变化：最小10px，最大15px */
}

        /* 左右布局容器 */
        .schedule-row {
            display: flex;
            gap: 20px;
            width: 100%;
        }

        /* 课表列样式 */
        .schedule-col {
            flex: 1;
            border: 1px solid #e5e7eb;
            border-radius: 8px;
            padding: 15px;
            background: #fff;
            transition: all 0.3s ease;
        }
        
        .schedule-col:hover {
            box-shadow: 0 4px 16px rgba(0,0,0,0.05);
        }

        /* 课表标题样式 */
        .schedule-col h2 {
            font-size: 14px; /* 原16px，调小2px */
            font-weight: 600;
            padding-bottom: 4px;   /* 底部内边距 */
            display: inline-block;
            margin-bottom: 5px;   /* 调整标题与表格的间距 */
            color: #165DFF;
            border-bottom: 2px solid #165DFF;
        }

        /* 表格样式（修改字体大小为自适应） */
        .schedule-table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0;
        table-layout: fixed;
        border-radius: 6px;
        overflow: hidden;
        }

        .schedule-table th {
            background: #f8fafc;
            font-weight: 600;
            color: #4b5563;
            padding: 10px 8px;
            text-align: center;
            vertical-align: middle;
            font-size: 14px;
            border: 1px solid #e2e8f0;
            border-bottom-width: 2px;
            font-size: clamp(0.5rem, 1.0vw, 0.8rem); /* 表格文字：最小0.7rem，最大1rem */
        }
        
        .schedule-table td {
            padding: 12px 8px;
            text-align: center;
            vertical-align: middle;
            font-size: 14px;
            border: 1px solid #e2e8f0;
            transition: background-color 0.2s ease;
            font-size: clamp(0.5rem, 0.9vw, 0.7rem); /* 表格文字：最小0.7rem，最大1rem */
        }
        
        /* 交替行颜色 */
        .schedule-table tr:nth-child(even) td {
            background-color: #f8fafc;
        }
        
        /* 课程类型颜色区分 */
        .schedule-table tr.course-type-1 td {
            background-color: rgba(230, 244, 255, 0.5);
        }
        
        .schedule-table tr.course-type-2 td {
            background-color: rgba(246, 255, 237, 0.5);
        }
        
        .schedule-table tr.course-type-3 td {
            background-color: rgba(255, 242, 232, 0.5);
        }
        
        .schedule-table tr.course-type-4 td {
            background-color: rgba(255, 240, 243, 0.5);
        }
        
        /* 悬停效果 */
        .schedule-table tr:hover td {
            background-color: rgba(22, 93, 255, 0.05);
        }

        /* 无数据提示 */
        .no-data {
            text-align: center;
            padding: 40px 0;
            color: #868e96;
            font-size: 14px;
            background-color: #f8fafc;
            border-radius: 6px;
        }

        /* 响应式适配 */
        @media (max-width: 768px) {
            .schedule-row {
                flex-direction: column;
            }
            .schedule-col {
                margin-bottom: 15px;
            }
        }
    </style>
</head>
<body class="font-inter bg-gray-50 text-dark overflow-hidden h-screen flex flex-col">
    <!-- 顶部标题区 -->
    <header class="bg-primary text-white py-2 shadow-md">
        <div class="container mx-auto px-4 text-center">
            <h1 class="text-xl md:text-2xl font-bold flex items-center justify-center">
                <i class="mdi mdi-flag mr-2"></i>
                十大队31队展示屏
            </h1>
        </div>
    </header>

    <!-- 顶部居中日期时间 -->
    <div class="top-center-box bg-primary/90 text-white py-1.5 rounded-b-lg">
        <div class="container mx-auto text-center font-medium" id="datetime"></div>
    </div>

    <!-- 主内容区 -->
    <main class="flex-1 overflow-hidden flex flex-col">
        <div class="flex-1 container mx-auto px-3 py-2 grid grid-cols-12 gap-3 overflow-hidden">
            <!-- 左侧：媒体+值班信息+重要通知 -->
            <div class="col-span-12 md:col-span-3 flex flex-col gap-3 h-full">
                <!-- 媒体轮播模块 -->
                <section id="carousel" class="relative bg-white rounded-xl shadow-sm overflow-hidden card-hover h-[254px]">
                    <h2 class="text-base font-bold text-dark p-3 bg-white z-10 relative">
                        <i class="mdi mdi-play-circle text-primary mr-2"></i>
                        媒体展示
                    </h2>
                    <div id="carousel-container" class="h-[calc(100%-42px)] relative">
                        <?php if (!empty($carouselItems)): ?>
                            <?php foreach ($carouselItems as $index => $item): ?>
                            <div class="carousel-item" style="opacity: <?php echo $index == 0 ? '1' : '0'; ?>; pointer-events: <?php echo $index == 0 ? 'auto' : 'none'; ?>">
                                <div class="absolute inset-0 bg-black/30 z-10"></div>
                                <?php if ($item['type'] == 'video'): ?>
                                <video class="w-full h-full object-cover" autoplay loop muted playsinline>
                                    <source src="<?php echo htmlspecialchars($item['url']); ?>" type="video/mp4">
                                    您的浏览器不支持视频播放
                                </video>
                                <?php else: ?>
                                <img src="<?php echo htmlspecialchars($item['url']); ?>" alt="<?php echo htmlspecialchars($item['title']); ?>" class="w-full h-full object-cover">
                                <?php endif; ?>
                                <div class="absolute bottom-0 left-0 right-0 p-3 z-20 text-white">
                                    <h3 class="text-sm font-bold mb-1 text-shadow"><?php echo htmlspecialchars($item['title']); ?></h3>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <div class="absolute inset-0 w-full h-full flex items-center justify-center">
                                <p class="text-gray-500">暂无媒体内容</p>
                            </div>
                        <?php endif; ?>
                    </div>
                    
                    <!-- 轮播控制按钮 -->
                    <?php if (count($carouselItems) > 1): ?>
                    <button id="prev-slide" class="absolute left-2 top-1/2 -translate-y-1/2 w-7 h-7 rounded-full bg-white/20 backdrop-blur-sm text-white flex items-center justify-center z-30 hover:bg-white/40 transition-colors">
                        <i class="mdi mdi-chevron-left text-xs"></i>
                    </button>
                    <button id="next-slide" class="absolute right-2 top-1/2 -translate-y-1/2 w-7 h-7 rounded-full bg-white/20 backdrop-blur-sm text-white flex items-center justify-center z-30 hover:bg-white/40 transition-colors">
                        <i class="mdi mdi-chevron-right text-xs"></i>
                    </button>
                    <div class="absolute bottom-2 left-1/2 -translate-x-1/2 flex gap-1.5 z-30">
                        <?php foreach ($carouselItems as $index => $item): ?>
                        <button class="w-2 h-2 rounded-full transition-colors" data-index="<?php echo $index; ?>" style="background-color: <?php echo $index == 0 ? 'white' : 'rgba(255,255,255,0.6)'; ?>"></button>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; ?>
                </section>

                <!-- 今日值班模块 -->
                <section class="bg-white rounded-xl shadow-sm overflow-hidden card-hover h-[254px]">
                    <h2 class="text-base font-bold text-dark p-2 border-b border-gray-100">
                        <i class="mdi mdi-calendar-check text-primary mr-2"></i>
                        今日值班
                    </h2>
                    <div class="p-3">
                        <?php if (!empty($dutyToday)): ?>
                            <div class="text-sm space-y-2">
                                <p><span class="text-gray-500">日期：</span><?php echo htmlspecialchars($dutyToday['duty_date']); ?>（第<?php echo htmlspecialchars($dutyToday['duty_week']); ?>周）</p>
                                <p><span class="text-gray-500">值班干部：</span><?php echo htmlspecialchars($dutyToday['duty_cadre']); ?></p>
                                <p><span class="text-gray-500">值班员：</span><?php echo htmlspecialchars($dutyToday['duty_officer']); ?></p>
                                <ul class="list-disc list-inside text-sm ml-2 space-y-1">
                                    <li>一区队：<?php echo htmlspecialchars($dutyToday['squad1']); ?></li>
                                    <li>二区队：<?php echo htmlspecialchars($dutyToday['squad2']); ?></li>
                                    <li>三区队：<?php echo htmlspecialchars($dutyToday['squad3']); ?></li>
                                    <?php if (!empty($dutyToday['squad4'])): // 仅当四区队有值时显示 ?>
                                    <li>四区队：<?php echo htmlspecialchars($dutyToday['squad4']); ?></li>
                                    <?php endif; ?>
                                    
                                </ul>
                            </div>
                        <?php else: ?>
                            <p class="text-gray-500 text-center py-4">暂无今日值班信息</p>
                        <?php endif; ?>
                    
                </section>
            
                <!-- 重要通知模块 -->
                <section class="bg-white rounded-xl shadow-sm overflow-hidden card-hover h-[254px]" id="important-notifications">
                    <h2 class="text-base font-bold text-dark p-2 border-b border-gray-100">
                        <i class="mdi mdi-alert-circle text-danger mr-2"></i>
                        重要通知
                    </h2>
                    <div class="p-3 overflow-y-auto scrollbar-hide h-[calc(100%-42px)]">
                        <?php if (!empty($importantNotifications)): ?>
                            <div class="space-y-4">
                                <?php foreach ($importantNotifications as $item): ?>
                                    <div class="border-l-2 border-danger pl-2 py-1">
                                        <h3 class="font-medium text-sm text-danger"><?php echo htmlspecialchars($item['title']); ?></h3>
                                        <p class="text-xs text-gray-500 mt-1 mb-2"><?php echo htmlspecialchars($item['publish_date']); ?></p>
                                        <p class="text-xs text-gray-600 line-clamp-3">
                                            <?php echo nl2br(htmlspecialchars($item['content'])); ?>
                                        </p>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php else: ?>
                            <p class="text-gray-500 text-center py-8">暂无重要通知</p>
                        <?php endif; ?>
                    </div>
                </section>
            </div>
            <!-- 中间：公告+课程表（课程表占比更大） -->
            <div class="col-span-12 md:col-span-6 flex flex-col gap-3 h-full">
                <!-- 通知公告模块（缩短） -->
                <section class="bg-white rounded-xl shadow-sm overflow-hidden card-hover h-[300px]"  id="notifications">
                    <h2 class="text-base font-bold text-dark p-2 border-b border-gray-100 flex justify-between items-center">
                        <div>
                            <i class="mdi mdi-bullhorn text-primary mr-2"></i>
                            通知公告
                        </div>
                        <span class="text-xs text-gray-500">更新于 <?php echo date('Y-m-d H:i'); ?></span>
                    </h2>
                    <div class="p-3 overflow-y-auto scrollbar-hide h-[calc(100%-42px)]">
                        <?php if (!empty($announcements)): ?>
                            <div class="space-y-4">
                                <?php foreach ($announcements as $item): ?>
                                    <div class="border-b border-gray-100 pb-3 last:border-0 last:pb-0">
                                        <div class="flex justify-between items-start mb-1">
                                            <h3 class="font-medium <?php echo $item['type'] === 'important' ? 'text-danger' : ''; ?>">
                                                <?php echo htmlspecialchars($item['title']); ?>
                                                <?php if ($item['type'] === 'important'): ?>
                                                    <span class="inline-block ml-2 text-xs bg-danger text-white px-1.5 py-0.5 rounded">重要</span>
                                                <?php endif; ?>
                                            </h3>
                                            <span class="text-xs text-gray-500"><?php echo htmlspecialchars($item['publish_date']); ?></span>
                                        </div>
                                        <p class="text-sm text-gray-600 line-clamp-2">
                                            <?php echo nl2br(htmlspecialchars($item['content'])); ?>
                                        </p>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php else: ?>
                            <p class="text-gray-500 text-center py-4">暂无公告信息</p>
                        <?php endif; ?>
                    </div>
                </section>
                
                <!-- 课程表模块（美化版，占比更大） -->
                <section id="course-schedule" class="bg-white rounded-xl shadow-sm overflow-hidden card-hover h-[470px]">
                    <div class="p-2 border-b border-gray-100">
                        <h2 class="text-base font-bold text-dark flex items-center">
                            <i class="mdi mdi-calendar text-primary mr-2"></i>
                            课程表
                        </h2>
                    </div>
                    
                    <!-- 原课程表代码替换为以下内容 -->
<div class="schedule-container">
    <div class="schedule-row">
        <!-- 左侧：今天课程 -->
        <div class="schedule-col">
            <h2><?php echo $weekdayMap[$todayWeekday] . '（' . date('m月d日', strtotime($todayDate)) . '）'; ?></h2>
            <?php if (empty($groupedToday)): ?>
                <div class="no-data">今日无课程安排</div>
            <?php else: ?>
                <table class="schedule-table">
                    <tr>
                        <th>时间</th>
                        <?php foreach ($todayClasses as $class): ?>
                            <th><?php echo $class; ?></th> <!-- 动态生成班级列 -->
                        <?php endforeach; ?>
                    </tr>
                    <?php 
                    $courseType = 1;
                    foreach ($groupedToday as $time => $classCourses): ?>
                        <tr class="course-type-<?php echo $courseType % 4 + 1; ?>">
                            <td><?php echo $time; ?></td> <!-- 时间行 -->
                            <?php foreach ($todayClasses as $class): ?>
                                <!-- 填充对应班级的课程 -->
                                <td><?php echo isset($classCourses[$class]) ? $classCourses[$class]['course_name'] : ''; ?></td>
                            <?php endforeach; ?>
                        </tr>
                        <?php $courseType++; ?>
                    <?php endforeach; ?>
                </table>
            <?php endif; ?>
        </div>

        <!-- 右侧：明天课程 -->
        <div class="schedule-col">
            <h2><?php echo $weekdayMap[$tomorrowWeekday] . '（' . date('m月d日', strtotime('+1 day')) . '）'; ?></h2>
            <?php if (empty($groupedTomorrow)): ?>
                <div class="no-data">明日无课程安排</div>
            <?php else: ?>
                <table class="schedule-table">
                    <tr>
                        <th>时间</th>
                        <?php foreach ($tomorrowClasses as $class): ?>
                            <th><?php echo $class; ?></th> <!-- 动态生成班级列 -->
                        <?php endforeach; ?>
                    </tr>
                    <?php 
                    $courseType = 1;
                    foreach ($groupedTomorrow as $time => $classCourses): ?>
                        <tr class="course-type-<?php echo $courseType % 4 + 1; ?>">
                            <td><?php echo $time; ?></td> <!-- 时间行 -->
                            <?php foreach ($tomorrowClasses as $class): ?>
                                <!-- 填充对应班级的课程 -->
                                <td><?php echo isset($classCourses[$class]) ? $classCourses[$class]['course_name'] : ''; ?></td>
                            <?php endforeach; ?>
                        </tr>
                        <?php $courseType++; ?>
                    <?php endforeach; ?>
                </table>
            <?php endif; ?>
        </div>
    </div>
</div>
                </section>
            </div>

            <!-- 右侧：奖励表彰+通报批评（删除了最新通知） -->
            <div class="col-span-12 md:col-span-3 flex flex-col gap-3 h-full">
                <!-- 奖励表彰模块 -->
                <section class="bg-white rounded-xl shadow-sm overflow-hidden card-hover h-[385px]" id="rewards">
                    <h2 class="text-base font-bold text-dark p-2 border-b border-gray-100">
                        <i class="mdi mdi-trophy text-warning mr-2"></i>
                        奖励表彰
                    </h2>
                    <div class="p-3 overflow-y-auto scrollbar-hide h-[calc(100%-42px)]">
                        <?php if (!empty($rewards)): ?>
                            <div class="space-y-4">
                                <?php foreach ($rewards as $item): ?>
                                    <div class="border-l-2 border-success pl-2 py-1">
                                        <h3 class="font-medium text-sm text-success"><?php echo htmlspecialchars($item['title']); ?></h3>
                                        <p class="text-xs text-gray-500 mt-1 mb-2"><?php echo htmlspecialchars($item['publish_date']); ?></p>
                                        <p class="text-xs text-gray-600 line-clamp-3">
                                            <?php echo nl2br(htmlspecialchars($item['content'])); ?>
                                        </p>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php else: ?>
                            <p class="text-gray-500 text-center py-8">暂无表彰信息</p>
                        <?php endif; ?>
                    </div>
                </section>

                <!-- 通报批评模块 -->
                <section class="bg-white rounded-xl shadow-sm overflow-hidden card-hover h-[385px]" id="punishments">
                    <h2 class="text-base font-bold text-dark p-2 border-b border-gray-100">
                        <i class="mdi mdi-alert text-danger mr-2"></i>
                        通报批评
                    </h2>
                    <div class="p-3 overflow-y-auto scrollbar-hide h-[calc(100%-42px)]">
                        <?php if (!empty($punishments)): ?>
                            <div class="space-y-4">
                                <?php foreach ($punishments as $item): ?>
                                    <div class="border-l-2 border-danger pl-2 py-1">
                                        <h3 class="font-medium text-sm text-danger"><?php echo htmlspecialchars($item['title']); ?></h3>
                                        <p class="text-xs text-gray-500 mt-1 mb-2"><?php echo htmlspecialchars($item['publish_date']); ?></p>
                                        <p class="text-xs text-gray-600 line-clamp-3">
                                            <?php echo nl2br(htmlspecialchars($item['content'])); ?>
                                        </p>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php else: ?>
                            <p class="text-gray-500 text-center py-8">暂无批评信息</p>
                        <?php endif; ?>
                    </div>
                </section>
            </div>
        </div>
    </main>

    <!-- 页脚 -->
    <footer class="bg-primary text-white py-1.5 text-center text-sm" onclick="window.location.href='login.php'">
        <p>值班电话:0371-81622464|队长赵国政:18638208832|教导员陈吉赟:15952087085|副队长郭家兴:13027601270</p>
    </footer>

    <!-- JavaScript -->
    <script>
        // 日期时间显示
        function updateDateTime() {
            const now = new Date();
            const options = { 
                year: 'numeric', 
                month: '2-digit', 
                day: '2-digit', 
                weekday: 'long',
                hour: '2-digit', 
                minute: '2-digit', 
                second: '2-digit' 
            };
            document.getElementById('datetime').textContent = now.toLocaleString('zh-CN', options);
        }
        updateDateTime();
        setInterval(updateDateTime, 1000);

        // 媒体轮播
        let currentSlide = 0;
        const slides = document.querySelectorAll('.carousel-item');
        const indicators = document.querySelectorAll('#carousel-container + div button');
        const slideCount = slides.length;

        function showSlide(index) {
            slides.forEach((slide, i) => {
                slide.style.opacity = i === index ? '1' : '0';
                slide.style.pointerEvents = i === index ? 'auto' : 'none';
            });
            indicators.forEach((ind, i) => {
                ind.style.backgroundColor = i === index ? 'white' : 'rgba(255,255,255,0.6)';
            });
            currentSlide = index;
        }

        function nextSlide() {
            showSlide((currentSlide + 1) % slideCount);
        }

        function prevSlide() {
            showSlide((currentSlide - 1 + slideCount) % slideCount);
        }

        if (slideCount > 1) {
            document.getElementById('next-slide').addEventListener('click', nextSlide);
            document.getElementById('prev-slide').addEventListener('click', prevSlide);
            indicators.forEach((ind, i) => {
                ind.addEventListener('click', () => showSlide(i));
            });
            setInterval(nextSlide, 5000); // 自动轮播
        }
        // 自动滚动功能实现
    function setupAutoScroll(containerClass, speed = 1, interval = 100) {  /* 每 20 毫秒滚动 1 像素*/ 
        const containers = document.querySelectorAll(containerClass);
        containers.forEach(container => {
            // 只有内容超出容器时才开启滚动
            if (container.scrollHeight <= container.clientHeight) return;
            
            let scrollPos = 0;
            const scrollInterval = setInterval(() => {
                scrollPos++;
                // 滚动到底部时重置到顶部
                if (scrollPos > container.scrollHeight - container.clientHeight) {
                    scrollPos = 0;
                }
                container.scrollTop = scrollPos;
            }, interval);
            
            // 鼠标悬停时暂停滚动，离开时继续
            container.addEventListener('mouseenter', () => {
                clearInterval(scrollInterval);
            });
            
            container.addEventListener('mouseleave', () => {
                setupAutoScroll(containerClass, speed, interval);
            });
        });
    }

    // 页面加载完成后初始化自动滚动
    window.addEventListener('load', () => {
        // 为三个版块分别设置自动滚动
        setupAutoScroll('#important-notifications .overflow-y-auto');
        setupAutoScroll('#rewards .overflow-y-auto');
        setupAutoScroll('#punishments .overflow-y-auto');
        setupAutoScroll('#notifications .overflow-y-auto'); // 新增通知公告
    });
    </script>
</body>
</html>